﻿using System;
using System.Security.Principal;

using LeanEngine.Entity;
using LeanEngine.Extensions;

namespace LeanEngine.Business.Security.Entity
{
    /// <summary>
    /// Represents a login information of a membership.
    /// </summary>
    [Serializable]
    public sealed class Token : IIdentity
    {
        #region Constructor Members

        /// <summary>
        /// Initialize a new instance of LeanEngine.Security.Token class.
        /// </summary>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        /// <param name="membership">The user acessing the application.</param>
        /// <param name="tokenID">The identifier of the login access.</param>
        /// <param name="authenticationSign">An unique key for the token.</param>
        public Token(Membership membership, Guid tokenID, int authenticationSign)
        {
            if (membership.IsNull()) throw new ArgumentNullException("membership");
            if (tokenID == Guid.Empty) throw new ArgumentException("The identification of the token cannot be a Guid.Empty.", "tokenID");

            this.TokenID = tokenID;
            this.UserName = membership.UserName;
            this.Password = membership.Password;
            this.Name = membership.DisplayName;
            this.AuthenticationSign = authenticationSign;
        }

        #endregion

        #region Property Members

        /// <summary>
        /// Gets the identifier of the login access.
        /// </summary>
        internal Guid TokenID
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the unique sign of the token.
        /// </summary>
        public int AuthenticationSign
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the membership user name.
        /// </summary>
        public string UserName
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the membership password.
        /// </summary>
        public string Password
        {
            get;
            private set;
        }

        #endregion

        #region IIdentity Members

        /// <summary>
        /// Gets the type of authentication used.
        /// </summary>
        public string AuthenticationType
        {
            get { return "LeanEngine_Authentication"; }
        }

        /// <summary>
        /// Gets a value that indicates whether the user has been authenticated.
        /// </summary>
        public bool IsAuthenticated
        {
            get { return true; }
        }

        /// <summary>
        /// Gets the name of the current user.
        /// </summary>
        public string Name
        {
            get;
            private set;
        }

        #endregion
    }
}
